package com.zerowidth.moc.web.controller.serviceimpl;

import com.zerowidth.moc.web.auth.TokenHelper;
import com.zerowidth.moc.web.controller.service.BabyService;
import com.zerowidth.moc.web.resp.ResCode;
import com.zerowidth.moc.web.resp.ResultBean;
import com.zerowidth.moc.web.resp.helper.ResponseCreator;
import com.zerowidth.db.bean.BabyBean;
import com.zerowidth.db.bean.RelationBean;
import com.zerowidth.db.bean.vm.BabyRelationVM;
import com.zerowidth.db.op.BabyRelationDB;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class BabyServiceImpl implements BabyService {

    @Override
    public ResponseEntity<ResultBean<BabyRelationVM>> createBaby(String creatorUserId, String relation, BabyBean babyBean) {
        BabyRelationVM vm = new BabyRelationVM();
        vm.baby_bean = babyBean;

        babyBean.baby_id = UUID.randomUUID().toString();
        vm.relation_bean = BabyRelationDB.createBabyAndRelation(creatorUserId, relation, babyBean);

        vm.family_list = BabyRelationDB.getBabyFamily(babyBean.baby_id);

        if (vm.relation_bean == null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30000, "创建宝宝失败");
        } else {
            return ResponseCreator.creatOk(vm, ResCode.CODE_OK, "创建宝宝成功");
        }
    }

    @Override
    public ResponseEntity<ResultBean<List<BabyRelationVM>>> getBabyRelationList(String user_id) {
        List<BabyRelationVM> vmList = BabyRelationDB.getBabyRelationVm(user_id);

        if (vmList == null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30010, "获取宝宝记录失败");
        } else {
            return ResponseCreator.creatOk(vmList, ResCode.CODE_OK, "获取宝宝记录成功");
        }
    }

    @Override
    public ResponseEntity<ResultBean<BabyRelationVM>> createInviteRelation(String user_id, String invite_code) {
        BabyRelationVM vm = new BabyRelationVM();

        vm.baby_bean = BabyRelationDB.findBaby(invite_code);
        if (vm.baby_bean == null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30020, "关注失败，邀请码不存在");
        } else if (vm.baby_bean.baby_status == 1) {
            return ResponseCreator.creatOk(null, ResCode.ER_30023, "关注失败，宝宝已被删除");
        } else if (vm.baby_bean.baby_status == 2) {
            return ResponseCreator.creatOk(null, ResCode.ER_30024, "关注失败，宝宝已被冻结");
        }

        if (BabyRelationDB.findRelation(user_id, vm.baby_bean.baby_id) != null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30021, "关注失败，你已经关注过，无需重复关注");
        }

        vm.relation_bean = BabyRelationDB.createInviteRelation(user_id, vm.baby_bean.baby_id, "亲友");

        vm.family_list = BabyRelationDB.getBabyFamily(vm.baby_bean.baby_id);

        if (vm.relation_bean == null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30022, "关注失败，创建亲友关系失败");
        } else {
            return ResponseCreator.creatOk(vm, ResCode.CODE_OK, "关注成功");
        }
    }

    @Override
    public ResponseEntity<ResultBean<Void>> setBabyTop(String babyId) {
        String userId = TokenHelper.getUserIdByToken();
        if (BabyRelationDB.updateTop(userId, babyId)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_OK, "置顶成功");
        } else {
            return ResponseCreator.creatOk(null, ResCode.ER_30032, "置顶失败");
        }
    }

    @Override
    public ResponseEntity<ResultBean<Void>> deleteBaby(String babyId) {
        String userId = TokenHelper.getUserIdByToken();
        if (BabyRelationDB.deleteBaby(userId, babyId)
                && BabyRelationDB.unFollowBaby(userId, babyId)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_OK, "删除成功");
        } else {
            return ResponseCreator.creatOk(null, ResCode.ER_30033, "删除失败");
        }
    }

    @Override
    public ResponseEntity<ResultBean<Void>> unFollowBaby(String babyId) {
        String userId = TokenHelper.getUserIdByToken();
        if (BabyRelationDB.unFollowBaby(userId, babyId)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_OK, "取消关注成功");
        } else {
            return ResponseCreator.creatOk(null, ResCode.ER_30033, "取消关注失败");
        }
    }

    @Override
    public ResponseEntity<ResultBean<Void>> modifyBabyInfo(String baby_id, String avatar, String nick_name, String birthday, int gender) {
        String userId = TokenHelper.getUserIdByToken();
        RelationBean relationBean = BabyRelationDB.findRelation(userId, baby_id);
        if (relationBean == null) {
            return ResponseCreator.creatOk(null, ResCode.ER_30030, "你不是这个宝宝的亲友，无权操作");
        }

        if (BabyRelationDB.updateBabyInfo(baby_id, avatar, nick_name, birthday, gender)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_OK, "宝宝资料保存成功");
        } else {
            return ResponseCreator.creatOk(null, ResCode.ER_30031, "宝宝资料保存失败");
        }
    }

}
